package com.jingdianjichi.home.controller;

import com.jingdianjichi.config.RestTemplateConfig;
import com.jingdianjichi.home.feign.SkuFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class TestController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private SkuFeignService skuFeignService;

    private static final String GET_SKU_INFO_URL = "http://APE-CLOUD-SKU:3015/test";
    @GetMapping("/test")
    public String test(){
        return "Hello Home";
    }


    @GetMapping("/getSkuInfo")
    public String getSkuInfo(){
        log.info("Home.getSkuInfo...");
        return restTemplate.getForObject(GET_SKU_INFO_URL,String.class);
    }

    @GetMapping("/getSkuInfoByFeign")
    public String getSkuInfoByFeign(){
        log.info("Home.getSkuInfoByFeign...");
        return skuFeignService.test();
    }

    @GetMapping("/testFeignTimeOut")
    public String testFeignTimeOut(){
        log.info("Home.testFeignTimeOut...");
        return skuFeignService.testTimeOut();
    }

    @GetMapping("/getHystrixTimeout")
    @HystrixCommand(fallbackMethod = "testHystrixTimeoutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "1000")
    })
    public String getHystrixTimeout() throws InterruptedException {
        return skuFeignService.testHystrixTimeout();
    }

    public String testHystrixTimeoutHandler()  {
        return "调用方主动降级了！!";
    }

}
